package com.miaoplus.swagger.controller;

import com.google.common.collect.Maps;
import com.miaoplus.swagger.model.Student;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Collection;
import java.util.concurrent.ConcurrentMap;

@RestController
@RequestMapping("/student")
@Api(tags = "学生接口")
public class HelloController {
    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
    public static ConcurrentMap<Integer, Student> repo = Maps.newConcurrentMap();
    
    @GetMapping("/list")
    @ApiOperation(value = "查看学生")
    public Collection<Student> get() {
        return repo.values();
    }
    
    @PostMapping
    @ApiOperation(value = "保存学生")
    public String save(@RequestBody Student student) {
        if(student != null && student.getId() != null) {
            repo.put(student.getId(), student);
            return "success";
        }
        return "fail";
    }
    
    @PutMapping
    @ApiOperation(value = "修改学生")
    public String update(@RequestBody Student student) {
        if(student != null && student.getId() != null) {
            Student exists = repo.get(student.getId());
            if(exists != null) {
                BeanUtils.copyProperties(student, exists);
            }
            return "success";
        }
        return "fail";
    }
    
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除学生")
    public String delete(@PathVariable("id") Integer id) {
        if(id != null) {
            repo.remove(id);
            return "success";
        }
        return "fail";
    }
    
}
